<?php
/**
 * Created by PhpStorm.
 * User: Aliaksei Lazerka
 * Date: 10/9/14
 * Time: 10:00 PM
 */

use exception\IException;

/**
 * @param IException $exception
 */
function handleException($exception)
{
  global $requestContext;
  $requestContext->addException($exception);
  include_once(dirname(__FILE__) . "/error.php");
  exit();
}

set_exception_handler('handleException');

$headers = null;
$requestContext = new \controller\RequestContext();

/**
 * Is used for setting variables in the request context
 * @param $key
 * @param $value
 */
function setContextObj($key, $value)
{
  global $requestContext;
  $requestContext->addObject($key, $value);
}

/**
 * Is used for getting variables from the request context
 * @param $key
 */
function getContextObj($key)
{
  global $requestContext;
  $requestContext->getObject($key);
}

/**
 * Is used for setting variables in the request context
 * @param $key
 * @param $value
 */
function throwError($key, $value)
{
  global $requestContext;
  $requestContext->addError($key, $value);
  include_once(dirname(__FILE__) . "/error.php");
  exit();
}

/**
 * @return array
 */
function getErrors()
{
  global $requestContext;
  return $requestContext->getErrors();
}

/**
 * @param string $key
 * @param string $value
 */
function addError($key, $value)
{
  global $requestContext;
  $requestContext->addError($key, $value);
}

/**
 * @return IException[]
 */
function getExceptions()
{
  global $requestContext;
  return $requestContext->getExceptions();
}

/**
 * @return bool
 */
function hasErrors()
{
  global $requestContext;
  return $requestContext->hasErrors();
}

if (!$_LZ_WEB_PARSER) {
  throw new \exception\WebParserNotFoundException();
}

$funController = \controller\Controller::getControllerInstance();

$funController->setParser($_LZ_WEB_PARSER);

$tmpArr = $funController->getActionAndFunctionFromUrl();
$actionName = $tmpArr['action'];
$actionFunc = $tmpArr['function'];
unset($tmpArr);

$action = $funController->getAction($actionName);

if ($action == null && $actionName == "") {
  $action = $funController->getAction("default");
}

if ($action == null) {
  throw new \exception\ActionNotFoundException($actionName);
}


if ($action->getAction()->getForward()) {
  $action = $funController->getAction($action->getAction()->getForward());
}

$modelBody = $action->getModel()->getBodyPath();
$viewBody = $action->getView()->getBodyPath();

if (!$modelBody) {
  throw new \exception\ModelBodyNotFoundException($action->getActionName());
}

if (!$viewBody) {
  throw new \exception\ViewBodyNotFoundException($action->getActionName());
}

ob_start();
foreach ($action->getFilters() as $filter) {
  if (!($filter instanceof \controller\AfterFilter)) {
    $filter->execute();
  }
}

ob_start();

foreach ($action->getModel()->getIncludes() as $include) {
  include_once($include);
}


include_once($modelBody);

ob_end_clean();


$headers = headers_list();

if ($actionFunc) {
  call_user_func($actionFunc);
}

foreach ($action->getView()->getIncludes() as $include) {
  include_once($include);
}

include_once($viewBody);


foreach ($action->getFilters() as $filter) {
  if ($filter instanceof \controller\AfterFilter) {
    $filter->execute();
  }
}

$header = array_merge($headers, headers_list());


if (!hasErrors()) {
  foreach ($headers as $key => $value) {
    header("$key: $value");
  }

  ob_end_flush();
} else {
  ob_end_clean();
  include_once(dirname(__FILE__) . "/error.php");
}